﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.Xpf.Grid;
using OpenLS.Spreadsheet;
using OpenLS.Spreadsheet.Formats;
using OpenLS.Spreadsheet.UI.Interfaces;
using QuantumWorkspace.SessionManager;

namespace Modularity
{
    public class StandardBigDataModule : IDataModule
    {
        private string _menuText;
        private JsonDataTableFormat _JsonTableStructure;


        public string MenuText
        {
            get { return _menuText; }
        }

        public Workbook CurrentWorkbook
        {
            get;
            set;
        }

        public GridControl CurrentGridControl
        {
            get;
            set;
        }

        private ObservableCollection<object> _DynamicGridSource;
        public ObservableCollection<object> DynamicGridSource
        {
            get { return _DynamicGridSource; }
            set
            {
                _DynamicGridSource = value;
            }
        }

        public void DefineTableJsonFormat(JsonDataTableFormat structure)
        {
            _JsonTableStructure = structure;
            var collection = CodeSession.Instance.GetDataGridRowsExternal(structure);
            Dictionary<string, string> columnNames = CodeSession.Instance.GetDataGridColumnsNames();

            DynamicGridSource = collection;
            // FORCE REFRESH
            CurrentGridControl.ItemsSource = DynamicGridSource;

            foreach (var colKeyValue in columnNames)
            {
                string strKey = colKeyValue.Key;
                string strLabel = colKeyValue.Value;

                int columnsNb = CurrentGridControl.Columns.Count;

                for (int colCounter = 0; colCounter < columnsNb; colCounter++)
                {
                    if (CurrentGridControl.Columns[colCounter].FieldName == strKey)
                        CurrentGridControl.Columns[colCounter].Header = strLabel;
                }
            }
        }


        public void CopyToSpreadsheet()
        {
            var activeSheet = this.CurrentWorkbook.GetActiveWorksheet();
            if (activeSheet != null)
            {
                var range = activeSheet.ActiveCell;
                int activeCellRowIndex = range.Row.Index;
                int activeCellColumnIndex = range.Column.Index;

                var viewsItems = this.CurrentGridControl.View.RootRowsContainer.Items;
                int nbColumns = this.CurrentGridControl.Columns.Count;
                int rowCounter = 0;
                foreach (var rowDataBase in viewsItems)
                {
                    var itemRow = ((DevExpress.Xpf.Grid.RowData)(rowDataBase)).Row;
                    for (int columnIndex = 0; columnIndex < nbColumns; columnIndex++)
                    {
                        var cellValue =
                                                        itemRow.GetType().GetProperty("Col" + columnIndex).GetValue(itemRow);

                        activeSheet[activeCellRowIndex + rowCounter, activeCellColumnIndex + columnIndex].Value = cellValue;

                    }
                    rowCounter++;
                    if (rowCounter > 5000)
                        break;
                }

            }
        }
    }
}
